using DIALOGUE;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace CMD
{
    public class CMD_DB_EXT_General:CommandDatabaseExtension
    {
        private static string[] PARAM_SPEED = new string[] { "-spd", "-speed" };
        private static string[] PARAM_IMMEDIATE = new string[] { "-i", "-immediate" };
        private static string[] PARAM_FILEPATH = new string[] { "-f", "-file","-filepath" };
        private static string[] PARAM_ENQUEUE = new string[] { "-e", "-enqueue" };
        new public static void Extend(CommandDatabase database)
        {
            database.Add("wait", new Func<string, IEnumerator>(Wait));

            //Dialogue Controls
            database.Add("showdialoguebox", new Func<string[],IEnumerator>(ShowDialogueBox));
            database.Add("hidedialoguebox", new Func<string[],IEnumerator>(HideDialogueBox));

            database.Add("showui", new Func<string[],IEnumerator>(ShowDialogueSystem));
            database.Add("hideui", new Func<string[],IEnumerator>(HideDialogueSystem));

            database.Add("load", new Action<string[]>(LoadNewDialogueFile));

        }

        private static IEnumerator Wait(string data)
        {
            //Debug.Log($"Wait call,data{data}");
            if (float.TryParse(data, out float time))
            {
                CommandSystem.instance.AddTerminationActionToCurrentProcess(() => { });
                yield return new WaitForSeconds(time);
            }
        }

        private static IEnumerator ShowDialogueBox(string[] data)
        {
            bool immediate;
            float speed;

            var parameters = ConvertDataToParameters(data);
            parameters.TryGetValue(PARAM_SPEED, out speed, defaultValue: 1f);
            parameters.TryGetValue(PARAM_IMMEDIATE, out immediate, defaultValue: false);
            yield return DialogueSystem.instance.dialogueContainer.Show(speed, immediate);
        }

        private static IEnumerator HideDialogueBox(string[] data)
        {
            bool immediate;
            float speed;

            var parameters = ConvertDataToParameters(data);
            parameters.TryGetValue(PARAM_SPEED, out speed, defaultValue: 1f);
            parameters.TryGetValue(PARAM_IMMEDIATE, out immediate, defaultValue: false);
            yield return DialogueSystem.instance.dialogueContainer.Hide(speed, immediate);
        }

        private static IEnumerator ShowDialogueSystem(string[] data)
        {
            bool immediate;
            float speed;

            var parameters = ConvertDataToParameters(data);
            parameters.TryGetValue(PARAM_SPEED, out speed, defaultValue: 1f);
            parameters.TryGetValue(PARAM_IMMEDIATE, out immediate, defaultValue: false);
            yield return DialogueSystem.instance.Show(speed,immediate);
        }

        private static IEnumerator HideDialogueSystem(string[] data)
        {
            bool immediate;
            float speed;

            var parameters = ConvertDataToParameters(data);
            parameters.TryGetValue(PARAM_SPEED, out speed, defaultValue: 1f);
            parameters.TryGetValue(PARAM_IMMEDIATE, out immediate, defaultValue: false);
            yield return DialogueSystem.instance.Hide(speed, immediate);
        }

        private static void LoadNewDialogueFile(string[] data)
        {
            string fileName = string.Empty;
            bool enQueue = false;

            var parameters = ConvertDataToParameters(data);

            parameters.TryGetValue(PARAM_FILEPATH, out fileName);
            parameters.TryGetValue(PARAM_ENQUEUE, out enQueue, defaultValue: false);

            string filePath = FilePath.GetPathToResource(FilePath.resource_dialogueFiles,fileName);
            TextAsset file = Resources.Load<TextAsset>(filePath);

            if(file == null)
            {
                Debug.LogWarning($"File '{filePath}' could not be loaded from dialouge files.Please ensure it exists within the '{FilePath.resource_dialogueFiles}' resources folder.");
                return;
            }

            List<string> lines = FileManager.ReadTextAsset(file, includeBankLines: true);
            Conversation newConversation = new Conversation(lines);

            if (enQueue)
                DialogueSystem.instance.conversationManager.Enqueue(newConversation);
            else
                DialogueSystem.instance.conversationManager.StartConversation(newConversation);

        }


    }
}